/**
 * Created with IntelliJ IDEA.
 * Description: synchronized锁是可重入的
 * User:岁亮亮
 * Date:2024-10-16
 * TIME:16:59
 */
//synchronized锁是可重入的

class Thread1 extends Thread{
    Object lock1 = new Object();
    Object lock2 = new Object();
    public int count = 0;
    public String lock3 = "jdj";
    @Override
    public void run() {
        if(Thread.currentThread().getName().equals("1")){
            try {
                sout();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        else {
            try {
                sout2();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }
    public void sout() throws InterruptedException {
        System.out.println("当前的线程是 " + Thread.currentThread().getName());
        synchronized (lock1){//当一个线程针对一个对象加锁之后 其他线程再对这个对象进行加锁就会进入阻塞状态等待该对象的锁释放为止
            count++;
            Thread.sleep(2);
            synchronized (lock3){//synchronized锁是可重入锁,只要加锁操作还是在这个线程中,就可再一次进行加锁
                System.out.println("dhfkal");
            }
        }
    }
    public void sout2() throws InterruptedException {
        System.out.println("当前的线程是 " + Thread.currentThread().getName());
        synchronized (lock3){

            count++;
            Thread.sleep(2000);
            synchronized (lock1){
                System.out.println("dhfkal");
            }
        }
    }

}
public class ThreadDemo2 {
    public static void main(String[] args) {
        Thread1 t = new Thread1();
        Thread t1 = new Thread(t,"1");
        Thread t2 = new Thread(t,"2");
        t1.start();
        t2.start();

    }
}
